package com.alibaba.basic.controller;

import com.alibaba.basic.businessexception.BusinessException;
import com.alibaba.basic.dto.SmsCodeDto;
import com.alibaba.basic.service.IverifyCodeService;
import com.alibaba.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IverifyCodeService verifyCodeService;

    // 将前端传过来的key用后端@PathVariable注解接受key
    @GetMapping("/image/{key}")
    public AjaxResult verifyCode(@PathVariable("key") String key) {
        try {

            // 将前端传过来的key获取64位Str
            String base64Str = verifyCodeService.getImgCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("获取失败");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统异常，请稍后重试");
        }
    }

    @PostMapping("/smsCode")
    public  AjaxResult smsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyCodeService.smsCode(smsCodeDto);
            return new AjaxResult();
            //只要有业务异常都要加
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("发送失败!"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取手机验证码失败");
        }
    }

    @PostMapping("/binderSmsCode")
    public  AjaxResult binderSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyCodeService.binderSmsCode(smsCodeDto);
            return new AjaxResult();
            //只要有业务异常都要加
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("发送失败!"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取手机验证码失败");
        }
    }
}
